package org.lookahead.parent.common.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * @Description BCryptPasswordEncoder加密工具类
 * Spring Security 提供的 BCryptPasswordEncoder 加密算法进行加密
 * @Author liucheng
 * @Date 2021/7/29 16:08
 * @Version 1.0
 */
public class BCryptUtil {

    public static void main(String[] args) {

        String aab = "     a       c      b     ";
        System.out.println(StringUtil.trimAllWhitespace(aab));

        int a = 7;
        int b = 6;
        DecimalFormat df = new DecimalFormat("0.00");
        BigDecimal bc = BigDecimal.valueOf((float) b / (float) a * 100);
        double f1 = bc.setScale(2, RoundingMode.HALF_UP).doubleValue();
        System.out.println(f1);

        String s = "0.12123123123123";
        String s2 = "0.12";
        String format = df.format(Double.parseDouble(s) + Double.parseDouble(s2));
        System.out.println(format);

        System.out.println(encode("la123456"));
        System.out.println(isMatch("la123456", "$2a$10$WzQJ41vK7SQkpIF5DqCQ5eJjwCmnlV5lKvy63BWEHNIHzLFDYRpA."));
        System.out.println(isMatch("cr123456", "$2a$10$Hff/2TP8kFxKhEMKkjF8iO4JcvKzp8K8RMDzu0gjzxXG9TgCM2pkK"));
    }

    /**
     * 对字符串加密
     *
     * @param str 加密对象字符串
     * @return 已加密结果字符串
     */
    public static String encode(String str) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder.encode(str);
    }

    /**
     * 验证密码是否和已加密对象字符串匹配
     *
     * @param passWord       密码字符串
     * @param encodePassWord 已加密对象字符串
     * @return
     */
    public static boolean isMatch(String passWord, String encodePassWord) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder.matches(passWord, encodePassWord);
    }

}
